﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GestureSaver;
using Microsoft.Kinect;
using System.IO;
using System.Globalization;

namespace PreProcessor
{
    static class Clipper
    {
        public static void Clip(GestureDB gestureDB, string newDirectory)
        {
            double threshold = 0.01;

            GestureDB newDb = new GestureDB();

            using (TextWriter writer = File.CreateText(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\movements.txt"))
            {
                foreach (List<Gesture> gestureClass in gestureDB)
                {
                    foreach (Gesture gestureSample in gestureClass)
                    {
                        // for all gesture samples ..                    

                        writer.WriteLine(gestureSample.inputFileName);

                        for (int i = 0; i < gestureSample.SkeletonCount - 1; i++)
                        {
                            double maxMovement = GetTotalMovement(gestureSample[i], gestureSample[i + 1]);

                            writer.WriteLine( maxMovement.ToString( CultureInfo.InvariantCulture) );
                        }

                        writer.Flush();
                    }
                }

                writer.Close();
            }
        }
        
        static private double GetMaxMovement(MSkeleton skel1, MSkeleton skel2)
        {
            double rVal = 0;

            foreach (JointType j in Enum.GetValues(typeof(JointType)))
            {
                double temp = EuclideanDistance(skel1[j], skel2[j]);

                if (temp > rVal)
                {
                    rVal = temp;
                }
            }

            return rVal;
        }

        static private double GetTotalMovement(MSkeleton skel1, MSkeleton skel2)
        {
            double rVal = 0;

            foreach (JointType j in Enum.GetValues(typeof(JointType)))
            {
                double temp = EuclideanDistance(skel1[j], skel2[j]);

                rVal += temp;
            }

            return rVal;
        }

        static double EuclideanDistance(MJoint j1, MJoint j2)
        {
            return Math.Sqrt(Math.Pow(j1.Position.X - j2.Position.X, 2) + Math.Pow(j1.Position.Y - j2.Position.Y, 2));        
        }

    }
}
